let app = getApp()
const checkUtil = require('../../../utils/checkUtil.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone: '',
        password: '',
        code: '',
        name: "",
        brandName: "",
        houseNo: "",
        market: "",

        //倒计时时间
        countDownTime: 300000,
        //显示发送验证码
        showCode: true,
        //显示倒计时
        showCountDown: false,
        //手机号格式错误提示
        phoneErrorMessage: "",
        //密码格式错误提示
        passwordErrorMessage: "",
        //验证码错误提示
        codeErrorMessage: "",
        active: 0,
        steps: [
            {
                text: '',
                desc: '注册',
            },
            {
                text: '',
                desc: '填写商家信息',
            },
            {
                text: '',
                desc: '完成',
            }
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let phone = options.phone
        if (phone && phone != '') {
            this.data.phone = phone
        }
    },
    nextStep: function () {
        let _this = this
        let data = {}
        if (this.data.phone == '') {
            this.setData({
                phoneErrorMessage: "请输入手机号"
            })
            return false;
        }
        if (this.data.password == '') {
            this.setData({
                passwordErrorMessage: "请输入密码"
            })
            return false;
        }
        if (this.data.code == '') {
            this.setData({
                codeErrorMessage: "请输入验证码"
            })
            return false;
        }
        data.phone = this.data.phone
        data.code = this.data.code
        app.formPost('/app/login/verifyCode', data).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                _this.setData({
                    active: 1
                })
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    phoneRegister: function () {
        let _this = this
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                if (res.code) {
                    let data = {}
                    data.code = res.code
                    data.phone = this.data.phone
                    data.password = this.data.password
                    data.name = this.data.name
                    data.brandName = this.data.brandName
                    data.houseNo = this.data.houseNo
                    data.marketName = this.data.market
                    wx.showLoading({
                        title: '注册中',
                    })
                    app.formPost('/app/login/register', data).then(res => {
                        _this.setData({
                            spinShow: false
                        });
                        wx.hideLoading()
                        if (res.code === 0) {
                            let data = res.data;
                            console.log("注册成功" + data.token)
                            wx.setStorageSync('token', data.token)
                            app.getUserMenu()
                            app.initUserData()
                            setTimeout(function () {
                                wx.switchTab({
                                    url: "/pages/index/index"
                                })
                                _this.setData({
                                    active: 2
                                })
                            }, 1000)
                        } else {
                            app.message(res.message, 'error')
                        }
                    }).catch(e => {
                        _this.setData({
                            spinShow: false
                        });
                        app.message(e, 'error')
                    })
                } else {
                    wx.showToast({
                        title: '获取code失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },
    sendSmsCode: function (e) {
        let _this = this
        let phone = this.data.phone
        if (phone == '') {
            this.setData({
                phoneErrorMessage: "请输入手机号"
            })
            return false;
        }
        if (!checkUtil.checkMobile(phone)) {
            this.setData({
                phoneErrorMessage: "手机号格式不正确"
            })
            return false;
        }
        let data = {}
        data.phone = this.data.phone
        app.formPost('/app/login/sendCode', data).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                app.message(res.message, 'success')
                _this.setData({
                    showCode: false,
                    showCountDown: true
                })
                const countDown = this.selectComponent('.control-count-down');
                countDown.start();
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    //倒计时结束
    finished: function () {
        this.setData({
            showCode: true,
            showCountDown: false
        })
    },
    toLogin: function () {
        wx.navigateTo({
            url: "/pages/login/index"
        })
    }
})